package com.example.fragmentbestpractice;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.UUID;

public class NewsTitleFragment extends Fragment {
    //这个片段是通过recyclerview的界面方式展现title的信息
    private boolean isTwoPane = false;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.news_title_frag, container, false);
        //匹配view中的recyclerView
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        RecyclerView recyclerView = view.findViewById(R.id.recyclerView_news_title);
        NewsAdapter adapter = new NewsAdapter(getNews());
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(manager);
        return view;
    }

    private ArrayList<News> getNews() {
        ArrayList<News> newsArrayList = new ArrayList<>();
        for(int i = 0; i < 100; i++){
            News news = new News();
            news.setTitle("Title:"+ i);
            news.setContent("Content"+ UUID.randomUUID());
            newsArrayList.add(news);
        }
        return newsArrayList;
    }

    //因为onActivityCreated方法给⚠️废弃了，所以在onStart中入手
    @Override
    public void onStart() {
        super.onStart();
        if(getActivity().findViewById(R.id.frameLayout_content) != null ){
            isTwoPane = true;
        }else{
            isTwoPane = false;
        }
    }
    // 数据生成



    //回顾最常用的recyclerView的用法，使用内部类进行编写
    class NewsAdapter extends RecyclerView.Adapter{
        //数据
        ArrayList<News> newsArrayList;
        //内部类再内部类
        class NewsViewHolder extends RecyclerView.ViewHolder{
            //这里承载的是textview这个标题
            TextView title;
            public NewsViewHolder(@NonNull View itemView) {
                super(itemView);
                title = itemView.findViewById(R.id.news_title);
            }
        }
        public NewsAdapter(ArrayList<News> newsArrayList) {
            this.newsArrayList = newsArrayList;
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_item,parent,false);
            final NewsViewHolder newsViewHolder = new NewsViewHolder(view);
            //双页模式点击事件
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //确定点击的位置
                    News news = newsArrayList.get(newsViewHolder.getAdapterPosition());//通过函数返回点击的位置
                    if(isTwoPane) {
                        //平板下的展现
                        //片段管理
                        NewsContentFragment contentFragment = (NewsContentFragment) getFragmentManager().findFragmentById(R.id.news_content_fragment);
                        contentFragment.refresh(news.getTitle(), news.getContent());
                    }else{
                        //活动的跳转
                        NewsContentActivity.startActivity(getActivity(),news.getTitle(),news.getContent());
                    }
                }
            });
            return  newsViewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
                News news = newsArrayList.get(position);
                ((NewsViewHolder) holder).title.setText(news.getTitle());
        }

        @Override
        public int getItemCount() {
            return newsArrayList.size();
        }
    }
}
